//
//  RingsController.m
//  Water
//
//  Created by yh on 2017/12/26.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "RingsController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <BlocksKit/BlocksKit+UIKit.h>
@interface RingsController (){
    NSArray *datas;
    SystemSoundID selectSound;
}

@end

@implementation RingsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"rings" ofType:@"plist"];
    datas = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    
    
    UIBarButtonItem *editBtn = [[UIBarButtonItem alloc]bk_initWithBarButtonSystemItem:UIBarButtonSystemItemSave handler:^(id sender) {

        if (self.save) {
            self.save(self.plan);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }];
    self.navigationItem.rightBarButtonItem = editBtn;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSString *name = datas[indexPath.row];
    cell.textLabel.text = [name stringByDeletingPathExtension];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    AudioServicesDisposeSystemSoundID(selectSound);
    selectSound = 0;
    NSString *name = datas[indexPath.row];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:[name stringByDeletingPathExtension] ofType:name.pathExtension];
    selectSound = kSystemSoundID_Vibrate;
    //这里使用在上面那个网址找到的铃声，注意格式
    NSString *path =filePath;
    if (path) {
        OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&selectSound);
        if (error != kAudioServicesNoError) {
            selectSound = 0;
        }
    }
    AudioServicesPlaySystemSound(selectSound);//播放声音
//  AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//静音模式下震动
    
    self.plan.notification.ring = name;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
